package com.bnq.logc.work

import android.content.Context
import com.bnq.logc.utils.ExecutorUtil
import java.util.concurrent.TimeUnit

interface IScheduledUpload {
    fun schedule(time:Long,unit: TimeUnit)
    fun cancel()
}

class WorkManagerScheduledUpload(val context: Context) : IScheduledUpload{
    override fun schedule(time: Long, unit: TimeUnit) {
        LogcUploadManager.getInstance(context).start(time,unit)
    }

    override fun cancel() {
        LogcUploadManager.getInstance(context).cancel()
    }
}

class ThreadPoolScheduledUpload : IScheduledUpload{

    private var id:String = "scheduled_upload"

    @Synchronized
    override fun schedule(time: Long, unit: TimeUnit) {
        val task = ExecutorUtil.getInstance().getScheduleTask(id)
        task?.cancel()
        ExecutorUtil.getInstance().scheduleAtFixedRate(ThreadUploadTask(),time,time,unit)
    }

    override fun cancel() {
        ExecutorUtil.getInstance().getScheduleTask(id)?.cancel()
    }
}